package com.morn.weibo.db;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.morn.weibo.types.Conversation;
import com.morn.weibo.types.Message;
import com.morn.weibo.types.Photo;
import com.morn.weibo.types.Status;
import com.morn.weibo.types.User;
import com.morn.weibo.types.UserAccount;

public class FanfouOpenHelper extends OrmLiteSqliteOpenHelper {

	private static final String DATABASE_NAME = "fanfou.db";
	private static final int DATABASE_VERSION = 2;

	private Dao<UserAccount, Integer> userAccountdao = null;

	private Dao<User, String> userDao = null;

	private Dao<Conversation, String> conversationDao = null;

	private Dao<Message, String> messageDao = null;

	public FanfouOpenHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	public FanfouOpenHelper(Context context, String databaseName,
			CursorFactory factory, int databaseVersion) {
		super(context, databaseName, factory, databaseVersion);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase database,
			ConnectionSource connectionSource) {
		try {
			TableUtils.createTable(connectionSource, UserAccount.class);
			TableUtils.createTable(connectionSource, Photo.class);
			TableUtils.createTable(connectionSource, User.class);
			TableUtils.createTable(connectionSource, Status.class);
			TableUtils.createTable(connectionSource, Message.class);
			TableUtils.createTable(connectionSource, Conversation.class);

		} catch (SQLException e) {
			Log.e(FanfouOpenHelper.class.getName(), "onUpgrade exception", e);
		}

	}

	@Override
	public void onUpgrade(SQLiteDatabase database,
			ConnectionSource connectionSource, int oldVersion, int newVersion) {
		try {
			TableUtils.dropTable(connectionSource, UserAccount.class, true);
			TableUtils.dropTable(connectionSource, Photo.class, true);
			TableUtils.dropTable(connectionSource, User.class, true);
			TableUtils.dropTable(connectionSource, Status.class, true);
			TableUtils.dropTable(connectionSource, Message.class, true);
			TableUtils.dropTable(connectionSource, Conversation.class, true);
			onCreate(database, connectionSource);
		} catch (SQLException e) {
			Log.e(FanfouOpenHelper.class.getName(), "onUpgrade exception", e);
		}

	}

	public Dao<UserAccount, Integer> getUserAccountDao() throws SQLException {
		if (userAccountdao == null) {
			userAccountdao = getDao(UserAccount.class);
		}
		return userAccountdao;
	}

	public Dao<User, String> getUserDao() throws SQLException {
		if (userDao == null) {
			userDao = getDao(User.class);
		}
		return userDao;
	}

	public Dao<Conversation, String> getConversationDao() throws SQLException {
		if (conversationDao == null) {
			conversationDao = getDao(Conversation.class);
		}
		return conversationDao;
	}

	public Dao<Message, String> getMessageDao() throws SQLException {
		if (messageDao == null) {
			messageDao = getDao(Message.class);
		}
		return messageDao;
	}
}